﻿$(document).ready(function() {
    JSUtility.url = '../../../Handler/AjaxHandle.ashx';
    JSHelper.combotreeProject('ProjectNum', "../Handler/AjaxProjectTreeHandle.ashx", 185);
    LoadReimbursementItem($("#CostType").combobox("getValue"));
    $('#btnCancel').click(JSUtility.close);
    $("#btnSubmit").bind("click", Add);
    $("#CostType").combobox({
        onChange: function(newValue, oldValue) {
            LoadReimbursementItem(newValue);
        }
    });
    if (jQuery.url.param("type") == "edit") {
        id = jQuery.url.param("ReimbursementId");
        operate = "edit";
        LoadReimbursement();
        LoadReimbursementPlaneTicket(); // 得到机票信息
        Loadbusinesstrip();
        $(".validatebox-tip").css("display", "none");
    } else {
        AddPlane();
        $("#ProjectNum").combotree({ required: true });
    }
    $("#ProjectNum").combotree("tree").tree({
        onClick: function(node) {
            if (node.attributes == null || node.attributes == 'undefined') {
                // $.messager.alert('系统提示', '此项不可选', 'info');
                $("#ProjectNum").combotree("setValue", null);
                $("#ProjectNum").combotree("setText", null);
            } else {
                $("#ProjectNum").combotree("setValue", node.attributes["ProjectNum"]);
                $("#ProjectNum").combotree("setText", node.text);
                $("#ProjectNum").combotree("hidePanel");
            }
        }
    });
});

var operate;
var id;

function Add() {
    operate = jQuery.url.param("type"); //操作类型
    //支出ID
    id = jQuery.url.param("ReimbursementId");
    var param = { AssemblyName: "OA.Bll.Reimbursement", ClassName: "OA.Bll.Reimbursement.ReimbursementPresenter", Method: "Add", ArrayOptions: new Array(), Options: {} };
    if (operate == "add") {
        param.Method = "Add";
    } else if (operate == "edit") {
        param.Method = "Update";
        param.Options["Id"] = id;
    }

    //得到报销内容信息
    var items = $("#Reimbursementform").serializeArray();
    $.each(items, function(i, item) {
        param.Options[item.name] = item.value;
    });
    //param.Options["EmployeeId"] = $("#EmployeeId").combotree('getValue');
    //报销内容信息结束
    var options = {};
    var count = parseInt($("#hidCount").val());
    for (var i = 0; i <= count; i++) {
        if ($("#" + i + "2").val() != "" && $("#" + i + "3").val() != "") {
            options = {};
            options["CostItemNo"] = $("#" + i + "0").html();
            options["CostItemName"] = $("#" + i + "1").html();
            options["BillCount"] = $("#" + i + "2").val();
            options["Money"] = $("#" + i + "3").val();
            options["Id"] = $("#" + i + "4").val();
            param.ArrayOptions.push(options);
        }
    }

    //获得机票信息开始
    //  options = {};
    //    var planeTicketItems = $("#PlaneTicketform").serializeArray();
    //    $.each(planeTicketItems, function(i, item) {

    //        options[item.name] = item.value;
    //    });

    for (var j = 0; j < planeArray.length; j++) {
        if ($("#StartPlace" + planeArray[j]).val() != "" && $("#ArrivePlace" + planeArray[j]).val() != ""
            && $("#BillCount" + planeArray[j]).val() != "" && $("#TotalMoney" + planeArray[j]).val() && $("#BusinessDate" + planeArray[j]).datebox("getText")) {
            options = {};
            options["type"] = "PlaneTicket";
            options["StartPlace"] = $("#StartPlace" + planeArray[j]).val();
            options["ArrivePlace"] = $("#ArrivePlace" + planeArray[j]).val();
            options["BillCount"] = $("#BillCount" + planeArray[j]).val();
            options["TotalMoney"] = $("#TotalMoney" + planeArray[j]).val();
            options["BusinessDate"] = $("#BusinessDate" + planeArray[j]).datebox("getText");
            options["Id"] = $("#Hidden" + planeArray[j]).val();
            param.ArrayOptions.push(options);
        }
    }
    // options = {};
    // options["type"] = "PlaneTicket";
    // param.ArrayOptions.push(options);
    //获得机票信息结束

    //获得出差信息开始
    options = {};
    var businesstripItems = $("#businesstripfrom").serializeArray();

    $.each(businesstripItems, function(i, item) {
        options[item.name] = item.value;
    });
    options["type"] = "businesstrip";
    param.ArrayOptions.push(options);
    //获得出差信息结束
    $.ajax({
        type: "post",
        url: JSUtility.url,
        data: $.toJSON(param),
        dataType: 'json',
        success: function(data) {
            $.messager.alert("信息提示", data.msg, "info");
        }
    });
}

function LoadReimbursement() {
    var param = { AssemblyName: "OA.Bll.Reimbursement", ClassName: "OA.Bll.Reimbursement.ReimbursementPresenter", Method: "Get", Options: {} };
    param.Options["ReimbursementId"] = id;
    $.ajax({
        type: "post",
        url: JSUtility.url,
        data: $.toJSON(param),
        dataType: 'json',
        success: function(data) {
            $('#Reimbursementform').form('load', data);
            $("#ProjectNum").combotree("setValue", data.ProjectNum);
            $("#ProjectNum").combotree("setText", data.ProjectName);
        }
    });

}
//加载机票信息
function LoadReimbursementPlaneTicket() {
    var param = { AssemblyName: "OA.Bll.Reimbursement", ClassName: "OA.Bll.Reimbursement.ReimbursementPlaneTicketPresenter", Method: "GetSearchAll", Options: {} };
    param.Options["ReimbursementId"] = id;
    $.ajax({
        type: "post",
        url: JSUtility.url,
        data: $.toJSON(param),
        dataType: 'json',
        success: function(data) {
            // $('#PlaneTicketform').form('load', data);
            $.each(data.ReimbursementPlaneTicketInfo, function(i, o) {
                AddPlane(o);
            });
        }
    });
}

function Loadbusinesstrip() {
    //    var param = { AssemblyName: "OA.Bll.Reimbursement", ClassName: "OA.Bll.Reimbursement.ReimbursementBusinessTravelPresenter", Method: "Get", Options: {} };
    //    param.Options["ReimbursementId"] = id;
    //    $.ajax({
    //        type: "post",
    //        url: JSUtility.url,
    //        data: $.toJSON(param),
    //        dataType: 'json',
    //        success: function(data) {
    //            var json = data;
    //            var count;
    //            $.each(json, function(i, o) {
    //                count = i;
    //                var result = "";
    //                result += "<tr><td><input id=\"" + i + "0\" name=\"" + i + "0\" class=\"easyui-validatebox\" style=\"width: 150px;\" disabled=\"true\" value=\"出发时间\" /></td>";
    //                result += "<td><input id=\"" + i + "1\" name=\"" + i + "1\" class=\"easyui-validatebox\" style=\"width: 150px;\"   />";
    //                result += "  </td><td><input id=\"" + i + "2\" name=\"" + i + "2\" class=\"easyui-validatebox\" style=\"width: 150px;\" disabled=\"true\" value=\"结束时间\" />";
    //                result += "</td><td><input id=\"" + i + "3\" name=\"" + i + "2\" class=\"easyui-validatebox\" style=\"width: 150px;\" /></td></tr>";
    //                $("#businesstrip").append(result);
    //                $("#" + i + "1").val(json[i].BusinessTravelStart);
    //                $("#" + i + "3").val(json[i].BusinessTraveEnd);
    //            });
    //        }
    //    });
    var param = { AssemblyName: "OA.Bll.Reimbursement", ClassName: "OA.Bll.Reimbursement.ReimbursementBusinessTravelPresenter", Method: "Get", Options: {} };
    param.Options["ReimbursementId"] = id;
    $.ajax({
        type: "post",
        url: JSUtility.url,
        data: $.toJSON(param),
        dataType: 'json',
        success: function(data) {
            $('#businesstripfrom').form('load', data);
        }
    });
}

function LoadReimbursementItem(costType) {
    var param = { AssemblyName: "OA.Bll.Reimbursement", ClassName: "OA.Bll.Reimbursement.ReimbursementDictionaryPresenter", Method: "GetSearchAll", Options: {} };
    //    if (operate == "edit") {
    //        param.ClassName = "OA.Bll.Reimbursement.ReimbursementItemPresenter";
    //        param.Options["ReimbursementId"] = id;
    //    }
    //    else {
    param.Options["CostType"] = costType;
    //}
    $.ajax({
        type: "post",
        url: JSUtility.url,
        data: $.toJSON(param),
        dataType: "json",
        success: function(data) {
            $("#ReimbursementDetail").empty();
            var json;
            //            if (operate == "edit") {
            //                json = data.ReimbursementItemInfo;
            //            }
            //            else {
            json = data.ReimbursementDictionaryInfo;
            // }
            $("#title").html(costType);
            var count;
            $("#ReimbursementDetail").html("<tr><td style=\"width: 205px;\" class='left1' align='center'> 编号</td><td class='top1' style=\"width: 205px;\">报销项目</td><td class='top1'  style=\"width: 205px\">票据张数</td><td class='top1'  style=\"width: 205px;\">报销金额</td></tr>");
            $.each(json, function(i, o) {
                count = i;
                var result = "";
                result += "<tr><td class='left2' align='center' ><span id=\"" + i + "0\" reimbursement='" + json[i].ItemNo + "' name=\"" + i + "0\" ></span></td>";
                result += "<td align='center'><span id=\"" + i + "1\" name=\"" + i + "1\"></span>";
                result += "  </td><td align='center'><input id=\"" + i + "2\" name=\"" + i + "2\" class=\"easyui-numberbox\" min='0'   style=\"width: 150px;\" />";
                result += "</td><td align='center'><input id=\"" + i + "3\" name=\"" + i + "3\" class=\"easyui-numberbox\" min='0' precision='4' style=\"width: 150px;\" /><input type=\"hidden\" id=\"" + i + "4\" name=\"" + i + "4\" /></td></tr>";
                $("#ReimbursementDetail").append(result);

                //                if (operate == "edit") {

                //                    $("#" + i + "0").val(json[i].CostItemNo);
                //                    $("#" + i + "1").val(json[i].CostItemName);
                //                    $("#" + i + "2").val(json[i].BillCount);
                //                    $("#" + i + "3").val(json[i].Money);
                //                    $("#Hidden" + i).val(json[i].Id);
                //                    $("#" + i + "2").numberbox({ min: 0 });
                //                    $("#" + i + "3").numberbox({ min: 0, precision: 4 });
                //                }
                //                else {

                $("#" + i + "0").html(json[i].ItemNo);
                $("#" + i + "1").html(json[i].ItemName);

                // }
                $("#" + i + "2").numberbox({ min: 0 });
                $("#" + i + "3").numberbox({ min: 0, precision: 4 });
            });
            if (operate == "edit") {
                LoadUpdateReimbursementItem();
            }
            $("#hidCount").val(count);
        }
    });
}

function LoadUpdateReimbursementItem() {
    var param = { AssemblyName: "OA.Bll.Reimbursement", ClassName: "OA.Bll.Reimbursement.ReimbursementDictionaryPresenter", Method: "GetSearchAll", Options: {} };
    param.ClassName = "OA.Bll.Reimbursement.ReimbursementItemPresenter";
    param.Options["ReimbursementId"] = id;
    $.ajax({
        type: "post",
        url: JSUtility.url,
        data: $.toJSON(param),
        dataType: "json",
        success: function(data) {

            var json = data.ReimbursementItemInfo;
            $.each(json, function(i, o) {
                var costitemNo = $("span[reimbursement='" + json[i].CostItemNo + "']").attr("Id");
                if (costitemNo != null && costitemNo != "" && costitemNo != 'undefined') {
                    costitemNo = costitemNo[0];
                    $("#" + costitemNo + "0").html(json[i].CostItemNo);
                    $("#" + costitemNo + "1").html(json[i].CostItemName);
                    $("#" + costitemNo + "2").val(json[i].BillCount);
                    $("#" + costitemNo + "3").val(json[i].Money);
                    $("#" + costitemNo + "4").val(json[i].Id);
                    $("#" + i + "2").numberbox({ min: 0 });
                    $("#" + i + "3").numberbox({ min: 0, precision: 4 });
                }
            });
        }
    });

}





var planeCount = 0;
var planeArray = new Array();
function AddPlane(data) {

    var planeTableTr = "<tr id='tr" + planeCount + "'><td align='center'  class='left2'> <input type=\"text\" id='StartPlace" + planeCount + "' name='StartPlace" + planeCount + "' class=\"easyui-validatebox\" /></td>"
                        + "<td align='center'><input type=\"text\" id='ArrivePlace" + planeCount + "' name='ArrivePlace" + planeCount + "' class=\"easyui-validatebox\" /></td>"
                        + "<td align='center'><input type=\"text\" id='BillCount" + planeCount + "' name='BillCount" + planeCount + "' class=\"easyui-validatebox\" /></td>"
                        + "<td align='center'><input type=\"text\" id='TotalMoney" + planeCount + "' name='TotalMoney" + planeCount + "' class=\"easyui-validatebox\" /></td>"
                        + "<td align='center'><input type=\"text\" id='BusinessDate" + planeCount + "' name='BusinessDate" + planeCount + "' />"
                        + "<td align='center'><input type=\"hidden\" id='Hidden" + planeCount + "' name='Hidden" + planeCount + "' />" +
                       "<a href=\"javascript:void()\" class=\"easyui-linkbutton\" id='del" + planeCount + "' iconcls=\"icon-remove\" onclick=\"$('#del" + planeCount + "').parent().parent().remove();planeArray.remove(" + planeCount + ");\" >删除</a></td></tr>";
    $("#PlaneTable").append(planeTableTr);
    $("#del" + planeCount).linkbutton({
        iconcls: 'icon-remove'
    });
    $("#BusinessDate" + planeCount).datebox();
    $("#BillCount" + planeCount).numberbox({ min: 0 });
    $("#TotalMoney" + planeCount).numberbox({ min: 0, precision: 4 });
    planeArray.push(planeCount);

    if (data != 'undefined' && data != null) {
        $("#StartPlace" + planeCount).val(data.StartPlace);
        $("#ArrivePlace" + planeCount).val(data.ArrivePlace);
        $("#BillCount" + planeCount).val(data.BillCount);
        $("#TotalMoney" + planeCount).val(data.TotalMoney);
        $("#BusinessDate" + planeCount).datebox("setText", data.BusinessDate);
        $("#Hidden" + planeCount).val(data.Id);
    }

    //    $("#del" + planeCount).bind("click", "del"+planeCount,function() {
    //        $(obj).parent().parent().parent().remove();
    //    });
    planeCount++;
}
function RemoveArray(array, attachId) {
    for (var i = 0, n = 0; i < array.length; i++) {
        if (array[i] != attachId) {
            array[n++] = array[i];
        }
    }
    array.length -= 1;
}
Array.prototype.remove = function(obj) {
    return RemoveArray(this, obj);
};